import { Suspense } from "react"
import NotFound from "../views/NotFound"
import { createBrowserHistory } from "history"
var history = createBrowserHistory()
export default function AuthComponent({ children, permission }) {
    // 判断用户token是否存在
    var token = localStorage.getItem("token");
    if (!token) {
        history.push('/login');
        history.go();
    }
    // 获取当前用户的权限列表
    var permissions = localStorage.getItem('userinfo') ?
        JSON.parse(localStorage.getItem('userinfo')).role.permissions : [];

    // 判断当前用户是否有权限访问该路由对应的组件
    var res = permission && permissions.includes(permission);
    return (
        <Suspense>
            {
                res ?
                    children :
                    <NotFound />
            }
        </Suspense>
    )

}